<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>旋转缩小动画</title>
    <style>
        .box1 {
            width: 200px;
            height: 200px;
            background-color: #bfa;
            transform-origin: center center;
        }

        .animate {
            animation: rotateAndShrink 1s forwards;
        }

        @keyframes rotateAndShrink {
            0% {
                transform: rotate(0deg) scale(1.5);
            }

            100% {
                transform: rotate(360deg) scale(1);
            }
        }
    </style>
</head>

<body>
    <button id="btn">点我一下</button>
    <hr />
    <div class="box1" id="box"></div>

    <script>
        window.onload = function () {
            const btn = document.getElementById('btn');
            const box = document.getElementById('box');
            /* 
                点击按钮后修改box1的宽度
                box.style.width = '400px';
                box.style.height = '400px';
            */

            btn.onclick = function () {
                // 设置初始放大（瞬间）
                box.style.transform = "scale(1.5)";

                // 触发动画（稍微延迟一下，确保 transform 生效）
                setTimeout(() => {
                    box.classList.add("animate");

                    // 动画完成后清除类名，方便再次触发
                    setTimeout(() => {
                        box.classList.remove("animate");
                        box.style.transform = ""; // 清除内联样式
                    }, 1000); // 和动画时间一致
                }, 50);
            };
        };
    </script>
</body>

</html>